﻿using System;
using System.Data;
using personalplaner.business.stamm;
using System.Collections.Generic;
using System.Linq;

namespace personalplaner.dataprovider.services
{
    public partial class PensumDataService
    {
        private static Pensum m_pensumData(DataTable table)
        {
            //bool prsidExists = table.Columns.Contains("prsid");
            //bool vonExists = table.Columns.Contains("von");
            //bool bisExists = table.Columns.Contains("bis");
            //bool pensumExists = table.Columns.Contains("pensum");

            //if (table.Rows.Count < 1)
            //    return new Pensum();

            //return new Pensum
            //{
            //    ID = prsidExists ? table.Rows[0].Field<int>("prsid") : int.MaxValue,
            //    Name = "pensum",
            //    Von = vonExists ? table.Rows[0].Field<DateTime>("von") : DateTime.MinValue,
            //    Bis = bisExists ? table.Rows[0].Field<DateTime>("bis") : DateTime.MinValue,
            //    Prozent = pensumExists ? table.Rows[0].Field<short>("pensum") : (short)100
            //};

            return m_pensenData(table).First();
        }

        private static IEnumerable<Pensum> m_pensenData(DataTable table)
        {
            bool prsidExists = table.Columns.Contains("prsid");
            bool vonExists = table.Columns.Contains("von");
            bool bisExists = table.Columns.Contains("bis");
            bool pensumExists = table.Columns.Contains("pensum");
            bool statusExists = table.Columns.Contains("status");
            bool statustextExists = table.Columns.Contains("statustext");

            foreach (DataRow row in table.Rows)
            {
                yield return new Pensum()
                {
                    ID = prsidExists ? row.Field<int>("prsid") : int.MaxValue,
                    Name = "pensum",
                    PrsID = prsidExists ? row.Field<int>("prsid") : 0,
                    Von = vonExists ? row.Field<DateTime>("von") : DateTime.MinValue,
                    Bis = bisExists ? row.Field<DateTime>("bis") : DateTime.MinValue,
                    Prozent = pensumExists ? row.Field<short>("pensum") : (short)100,
                    Status = statusExists ? row.Field<byte>("status") : (byte)0,
                    StatusText = statustextExists ? row.Field<string>("statustext") : string.Empty,
                };
            }
        }
    }
}
